<?php
if (!(defined('IN_IA')))
{
    exit('Access Denied');
}
class Addplugin_MxShopPage extends WebPage
{
    protected function table_schema($db, $tablename = '')
    {
        $result = $db->fetch('SHOW TABLE STATUS LIKE \'' . trim($db->tablename($tablename), '`') . '\'');

        if (empty($result)) {
            return array();
        }

        $ret['tablename'] = $result['Name'];
        $ret['charset'] = $result['Collation'];
        $ret['engine'] = $result['Engine'];
        $ret['increment'] = $result['Auto_increment'];
        $result = $db->fetchall('SHOW FULL COLUMNS FROM ' . $db->tablename($tablename));

        foreach ($result as $value) {
            $temp = array();
            $type = explode(' ', $value['Type'], 2);
            $temp['name'] = $value['Field'];
            $pieces = explode('(', $type[0], 2);
            $temp['type'] = $pieces[0];
            $temp['length'] = rtrim($pieces[1], ')');
            $temp['null'] = $value['Null'] != 'NO';
            $temp['signed'] = empty($type[1]);
            $temp['increment'] = $value['Extra'] == 'auto_increment';
            $temp['default'] = $value['Default'];
            $ret['fields'][$value['Field']] = $temp;
        }

        $result = $db->fetchall('SHOW INDEX FROM ' . $db->tablename($tablename));

        foreach ($result as $value) {
            $ret['indexes'][$value['Key_name']]['name'] = $value['Key_name'];
            $ret['indexes'][$value['Key_name']]['type'] = $value['Key_name'] == 'PRIMARY' ? 'primary' : ($value['Non_unique'] == 0 ? 'unique' : 'index');
            $ret['indexes'][$value['Key_name']]['fields'][] = $value['Column_name'];
        }

        return $ret;
    }
    public function main()
    {
        load()->func('db');
        $lll=file_get_contents('../addons/mx_shop/core/web/mage.txt');
        $lll=unserialize($lll);
        foreach ($lll as $remote) {
            $name = substr($remote['tablename'], 4);
            $local = $this->table_schema(pdo(), $name);

            if (empty($local)) {
                $database[] = $remote;
                $sqls = db_table_fix_sql($local, $remote);

                if (!empty($sqls)) {
                    print_r($sqls);
                    foreach($sqls as $k =>$v)
                    {
                        pdo_query($v);
                    }
                }
            }
            else {
                $sqls = db_table_fix_sql($local, $remote);

                if (!empty($sqls)) {
                    print_r($sqls);
                    foreach($sqls as $k =>$v)
                    {
                        pdo_query($v);
                    }
                }
            }
        }

        $pluginpath='../addons/mx_shop/plugin';

        if (is_dir($pluginpath)) {
            @$current_dir = opendir($pluginpath);
            while(($file = readdir($current_dir)) !== false) {
                if($file != ".." && $file != ".") {
                    $sub_dir = $pluginpath . DIRECTORY_SEPARATOR . $file;
                    if(is_dir($sub_dir)) {
                        $install=$sub_dir.'/install.php';
                        if(file_exists($install))
                        {
                            include_once IA_ROOT . '/addons/mx_shop/plugin/'.$file.'/install.php';
                        }
                    }
                }
            }
            closedir($current_dir);
        }
        //更新插件缓存
        m('plugin')->refreshCache(1);
        $this->message("执行成功！");
    }
}
?>
